﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebSite_m_data_testresult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "2")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
        return;
    }
    protected void InsertBtn_Click(object sender, EventArgs e)
    {
        try{
            //获得用户要插入的信息。
            string userid = UserIdTextBox.Text;
            if (userid.Length==0)
            {
                WarnLabel.Text = "用户ID不能为空";
                WarnLabel.Visible = true;
                return;
            }
            string Weight = WeightTextBox.Text;
            string height = HeightTextBox.Text;
            string vital = VitalCapacityTextBox.Text;
            string StaminaName = StaminaDropDownList.SelectedItem.Text;
            string Stamina = StaminaTextBox.Text;
            string StrengthName = StrengthDownList.SelectedItem.Text;
            string Strength = StrengthTextBox.Text;
            string SpeedName = SpeedDropDownList.SelectedItem.Text;
            string Speed = SpeedTextBox.Text;
            string TestDate=DateTime.Now.Year.ToString()+"-1-1"; //即默认为测试时间是插入信息那年的第一天
           
            string sqlString = "select UserId from UserInfo where UserId=" + userid;
            SqlConnection conn = new SqlConnection("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            conn.Open();
            SqlCommand command = new SqlCommand(sqlString, conn);

            SqlDataReader reader = command.ExecuteReader();
            if (!reader.HasRows)
            {
                WarnLabel.Text = "该用户id不存在";
                WarnLabel.Visible = true;
                return;
            }

            //更新数据，如果该年度该生的数据已经存在，会报错的，因为我用的可是insert插入。
            //没有验证是否该数据存在，我觉得既然可以修改数据，那么就没有必要再写这个了。
            reader.Dispose();
            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values("+userid+",'"+TestDate+"','身高',"+height+")";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();

            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values(" + userid + ",'" + TestDate + "','体重'," + Weight + ")";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();

            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values(" + userid + ",'" + TestDate + "','肺活量'," + vital + ")";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();

            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values(" + userid + ",'" + TestDate + "','"+StaminaName+"'," + Stamina + ")";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();

            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values(" + userid + ",'" + TestDate + "','" + StrengthName + "'," + Strength + ")";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();

            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values(" + userid + ",'" + TestDate + "','" + SpeedName + "'," + Speed + ")";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();

            sqlString = "insert into PhysiqueTest(UserId,TestDate,TestName,Result) values(" + userid + ",'" + TestDate + "','" + OtherDropDownList.SelectedItem.ToString() + "',0)";
            command.CommandText = sqlString;
            command.ExecuteNonQuery();
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "m_data_testresult.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }

        
    }
}
